﻿using System;
using System.Text.RegularExpressions;
using System.Web;


namespace DBA.Common
{
    public class HtmlHelper
    {
        public static string Encode(string input)
        {
            if (VerificationHelper.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            input = input.Replace("&", "&amp;");
            input = input.Replace("<", "&lt;");
            input = input.Replace(">", "&gt");
            input = input.Replace("'", "''");
            input = input.Replace("\n", "<br/>");
            input = input.Replace("\r\n", "<br/>");
            return input;
        }

        public static string GetNumber(decimal input, int length)
        {
            return Math.Round(input, length).ToString();
        }

        public static string GetShortDateString(object obj)
        {
            DateTime time;
            if ((obj != null) && DateTime.TryParse(obj.ToString(), out time))
            {
                return time.ToString("yyyy-MM-dd");
            }
            return "";
        }

        public static string NoHTML(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                input = Regex.Replace(input, "<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "-->", "", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "<!--.*", "", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(quot|#34);", "\"", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(amp|#38);", "&", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(lt|#60);", "<", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(gt|#62);", ">", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(iexcl|#161);", "\x00a1", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(cent|#162);", "\x00a2", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(pound|#163);", "\x00a3", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, "&(copy|#169);", "\x00a9", RegexOptions.IgnoreCase);
                input = Regex.Replace(input, @"&#(\d+);", "", RegexOptions.IgnoreCase);
                input = HttpContext.Current.Server.HtmlEncode(input).Trim();
            }
            return input;
        }

        public static string ReFormatHtml(string input)
        {
            if (VerificationHelper.IsNullOrEmpty(input))
            {
                return input;
            }
            string str = input;
            return str.Replace("<br/>", "\r\n").Replace("&nbsp;", " ");
        }
    }
}